<?php
namespace app\index\controller;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

use app\common\Check;
use app\common\Tools;
use think\Console;
use think\Db;
use think\facade\Config;
use think\facade\Request;

class Index
{
    public function index()
    {
        header('Location: /user');
    }

    /**
     * 用户请求短信验证码
     */
    public function mb_UserGetCode()
    {
        $Device = Request::has('Device') ? Request::param('Device') : '';
        $Type = Request::has('Type') ? Request::param('Type') : 0;
        $Sign = Request::has('Sign') ? Request::param('Sign') : 0;  //md5(Date(YmdHi)+Device+Config_Sign)  防盜鏈(1分鐘有效時間)

        if (!$Type) mb_OutMessage(ErrCode_Toast, '参数有误');
        $ProjectName = Config::get('project')['name'];

        $Code = rand(100000, 999999);
        $Body = "[$ProjectName]您的验证码为 $Code ，有效期为5分钟，请确保是本人操作，不要把验证码泄露给其他人。";

        if ($Type == 1) {
            if (!$Device) mb_OutMessage(ErrCode_Toast, '请输入手机号码');
            if (!Check::IsChinaPhone($Device)) mb_OutMessage(ErrCode_Toast, '请输入有效的手机号码');

            $CaptchaData = Db::table('captcha')->field('ID,TimeCreated')->where("Device='$Device' AND IsDeleted='0'")->order('ID','DESC')->find();
            if ($CaptchaData != null && (time() - strtotime($CaptchaData['TimeCreated']) < 300))
                mb_OutMessage(ErrCode_Toast,'您已发送验证码，请勿重复发送');

            Db::startTrans();
            try {
                Db::table('captcha')->insert(['Device' => $Device, 'Code' => $Code]);
                Db::table('system_sms')->insert(['To'=>$Device,'Body'=>$Body]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                mb_OutMessage(ErrCode_Toast, $e->getMessage());
            }
            mb_OutMessage(ErrCode_Normal,'获取验证码成功');

        } else if ($Type == 2) {
            if (!$Device) mb_OutMessage(ErrCode_Toast, '请输入电子邮箱');
            if (!Check::IsEmail($Device)) mb_OutMessage(ErrCode_Toast, '请输入有效的电子邮箱');

            $CaptchaData = Db::table('captcha')->field('ID,TimeCreated')->where("Device='$Device' AND IsDeleted='0'")->order('ID','DESC')->find();
            if ($CaptchaData != null && (time() - strtotime($CaptchaData['TimeCreated']) < 300))
                mb_OutMessage(ErrCode_Toast,'您已发送验证码，请勿重复发送');

            Db::startTrans();
            try {
                Db::table('captcha')->insert(['Device' => $Device, 'Code' => $Code]);
                Db::table('system_email')->insert(['Email'=>$Device,'Body'=>$Body]);
                Tools::mb_SendEmail(array(['address'=>$Device,'name'=>'']),'小时光科技 - 验证码',$Body);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                mb_OutMessage(ErrCode_Toast, $e->getMessage());
            }
            mb_OutMessage(ErrCode_Normal,'获取验证码成功');
        }
    }

    /**
     * 用户验证短信验证码
     */
    public function mb_UserCommitCode()
    {
        $Device = Request::has('Device') ? Request::param('Device') : '';
        $Code = Request::has('Code') ? Request::param('Code') : '';
        if(!$Code)mb_OutMessage(ErrCode_Toast,'请输入验证码');

        $CaptchaData = Db::table('captcha')->field('ID,TimeCreated')->where("IsDeleted='0' AND Device='$Device' AND Code='$Code'")->order('ID','DESC')->find();
        if($CaptchaData == null || (time() - strtotime($CaptchaData['TimeCreated']) > 300) )mb_OutMessage(ErrCode_Toast,'验证码不正确');

        Db::startTrans();
        try {
            Db::table('captcha')->update(['ID'=>$CaptchaData['ID'],'IsDeleted' => 1]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'验证成功');
    }

    /**
     * 公司短信发送器获取一条记录
     */
    public function mb_SenderGetMessage()
    {
        $TimeCreated = date('Y-m-d H:i:s', time() - 300);
        $SMS = Db::table('system_sms')->field('ID,To,Body')->where("IsDeleted='0' AND IsSent='0' AND TimeCreated>'$TimeCreated'")->findOrEmpty();
        mb_OutMessage(ErrCode_Normal,'',$SMS);
    }

    /**
     * 公司短信发送器确认发送一条短信
     */
    public function mb_SenderSetMessage()
    {
        $ID = Request::has('ID') ? Request::param('ID') : 0;
        if (!$ID) mb_OutMessage(ErrCode_Toast, '参数错误');
        Db::startTrans();
        try {
            Db::table('system_sms')->update(['ID'=>$ID,'IsSent'=>1]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal,'');
    }
}
